package websocketInstance;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/echo")//加上这个注解会被扫描加入set集合
public class EchoEndpoint {
    public EchoEndpoint(){
        System.out.println("xxx");
    }
    @OnOpen
    public void open(Session session){
        System.out.println("Session"+"  "+session.getId());
    }
    @OnClose
    public void close(Session session){
        System.out.println("close"+"  "+session.getId());
    }
    @OnMessage
    public void message(Session session,String message){
        System.out.println(session.getId()+"  "+message );
        //服务端给客户端返回消息
        try {
            session.getBasicRemote().sendText("你好");
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }
}
